using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace TTTView
{
    public delegate void BoxHandler(Box box);

    public class Boxes : IEnumerable
    {
        private TttButton[,] buttons;
        private bool turn = false;

        public event BoxHandler onPlace;

        public Boxes()
        {
            buttons = new TttButton[3, 3];
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    buttons[i, j] = new TttButton(i, j);
                    buttons[i, j].Size = new Size(100, 100);
                    buttons[i, j].Location = new Point(j * 100, i * 100);
                    buttons[i, j].Click += new EventHandler(Boxes_Click);
                }
            }
        }

        private void Boxes_Click(object sender, EventArgs e)
        {
            TttButton btn = (TttButton)sender;
            
            RaisePlaceEvent(btn.Box);

            if (turn)
            {
                btn.Image = Image.FromFile("..\\..\\images\\cross.gif");
                turn = false;
            }
            else
            {
                btn.Image = Image.FromFile("..\\..\\images\\tick.gif");
                turn = true;
            }
        }

        public TttButton this[Box box]
        {
            get 
            {
                return buttons[box.Col - 1, box.Row - 1];
            }
        }

        #region IEnumerable Members

        public IEnumerator GetEnumerator()
        {
            return buttons.GetEnumerator();
        }

        #endregion

        private void RaisePlaceEvent(Box box)
        {
            if (onPlace != null)
            {
                onPlace(box);
            }
        }
    }
}
